package com.hd.trans.files.domain;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.hd.trans.files.bean.Video;
import com.hd.trans.files.model.FileSortModel;
import com.hd.trans.files.repository.LocalVideoProvider;
import com.hd.trans.files.repository.VideoDataRepository;
import com.hd.trans.files.sort.MediaSortHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.core.SingleOnSubscribe;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Author: yangtianwen
 * Time: 2021/12/16
 * Des: 搜索视频业务处理
 */
public class SearchVideoUseCase extends BaseUseCase {

    private final VideoDataRepository mVideoDataRepository;

    private Disposable mSearchDisposable;

    private MutableLiveData<List<Video>> mSearchVideoObserver;
    private MutableLiveData<String> mScanPathObserver;
    private MutableLiveData<Boolean> mLoadingObserver;

    public SearchVideoUseCase() {
        mVideoDataRepository = new VideoDataRepository();
    }

    /**
     * 视频搜索
     */
    public void searchVideo(List<Video> videoData, String keyWord, String lastKeyWord,
                            FileSortModel.ModeItem sortModeItem,
                            Callback<String> callback) {
        if (mSearchDisposable != null) mSearchDisposable.dispose();
        mSearchDisposable = Single.create(
                (SingleOnSubscribe<List<Video>>) emitter -> taskSearchVideo(
                        videoData, keyWord, lastKeyWord, sortModeItem))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(videos -> {
                    if (callback != null) callback.onValueChange(keyWord);
                });
    }

    private void taskSearchVideo(List<Video> videoData, String keyWord, String lastKeyWord, FileSortModel.ModeItem sortModeItem) {
        final List<Video> searchVideo = new ArrayList<>();
        if (!keyWord.isEmpty() && TextUtils.equals(keyWord, lastKeyWord)) {
            // 防止相同关键词多次查询
            return;
        }
        if (TextUtils.isEmpty(keyWord)) {
            // 没有搜索关键词时显示原来的列表数据
            if (mSearchVideoObserver != null) mSearchVideoObserver.postValue(videoData);
            return;
        }
        for (Video video : videoData) {
            if (video.getName().toUpperCase().contains(keyWord.toUpperCase())) {
                searchVideo.add(video);
            }
        }
        if (mSearchVideoObserver != null) mSearchVideoObserver.postValue(
                MediaSortHelper.getInstance().sortVideo(searchVideo, sortModeItem)
        );
    }

    public void searchVideoInDirectory(List<String> scanDir, String keyWord, FileSortModel.ModeItem sortModeItem) {
        if (mLoadingObserver != null) mLoadingObserver.setValue(true);
        if (mSearchDisposable != null) mSearchDisposable.dispose();
        mSearchDisposable = mVideoDataRepository.searchVideoWithKeyword(scanDir, keyWord, sortModeItem,
                new LocalVideoProvider.ScanFileInterface() {
                    @Override
                    public void scanFilePath(String path) {
                        if (mScanPathObserver != null) mScanPathObserver.postValue(path);
                    }

                    @Override
                    public void scanNewFileNum(int newNum) {

                    }
                }).subscribe(videos -> {
            if (mLoadingObserver != null) mLoadingObserver.setValue(false);
            if (mSearchVideoObserver != null) mSearchVideoObserver.setValue(videos);
        });
    }

    public SearchVideoUseCase addScanPathObserver(MutableLiveData<String> scanPathObserver) {
        this.mScanPathObserver = scanPathObserver;
        return this;
    }

    public void addLoadingObserver(MutableLiveData<Boolean> loadingObserver) {
        this.mLoadingObserver = loadingObserver;
    }

    public SearchVideoUseCase addSearchVideoObserver(MutableLiveData<List<Video>> searchVideoObserver) {
        this.mSearchVideoObserver = searchVideoObserver;
        return this;
    }

    public void cancelSearch() {
        if (mSearchDisposable != null) {
            mSearchDisposable.dispose();
        }
    }
}
